﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Scribd.Net
{
    public static class Extensions
    {

        /// <summary>
        /// Returns the Flash embedding code for this document.
        /// </summary>
        /// <param name="document">The source document to embed.</param>
        /// <param name="startingPage">The page to start on.</param>
        /// <param name="viewMode">The way the pages should be displayed.</param>
        /// <param name="viewHeight">The height of the embeded document.</param>
        /// <param name="viewWidth">The width of the embeded document.</param>
        /// <returns>String</returns>
        public static string GetEmbedCode(this Document document, int startingPage, ViewMode viewMode, string viewHeight, string viewWidth)
        {
            string _viewMode = Enum.GetName(typeof(ViewMode), viewMode);
            if (string.IsNullOrEmpty(viewHeight)) { viewHeight = "500"; }
            if (string.IsNullOrEmpty(viewWidth)) { viewWidth = "100%"; }
            if (startingPage < 1) { startingPage = 1; }

            StringBuilder _sb = new StringBuilder();

            _sb.AppendFormat(@"<object codebase=""http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"" id=""doc_296323{0}"" name=""doc_296323{0}"" classid=""clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"" align=""middle"" height=""500"" width=""100%"">", document.DocumentId.ToString());
            _sb.AppendFormat(@"<param name=""movie"" value=""http://documents.scribd.com/ScribdViewer.swf?document_id={0}&access_key={1}&page={2}&version={3}&auto_size=true&viewMode={4}"">", document.DocumentId.ToString(), document.AccessKey, startingPage.ToString(), "1", _viewMode);
            _sb.Append(@"<param name=""quality"" value=""high"">");
            _sb.Append(@"<param name=""play"" value=""true"">");
            _sb.Append(@"<param name=""loop"" value=""true"">");
            _sb.Append(@"<param name=""scale"" value=""showall"">");
            _sb.Append(@"<param name=""wmode"" value=""opaque"">");
            _sb.Append(@"<param name=""devicefont"" value=""false"">");
            _sb.Append(@"<param name=""bgcolor"" value=""#ffffff"">");
            _sb.Append(@"<param name=""menu"" value=""true"">");
            _sb.Append(@"<param name=""allowFullScreen"" value=""true"">");
            _sb.Append(@"<param name=""allowScriptAccess"" value=""always"">");
            _sb.Append(@"<param name=""salign"" value="""">");
            _sb.AppendFormat(@"<embed src=""http://documents.scribd.com/ScribdViewer.swf?document_id={0}&access_key={1}&page={2}&version={3}&auto_size=true&viewMode={4}"" quality=""high"" pluginspage=""http://www.macromedia.com/go/getflashplayer"" play=""true"" loop=""true"" scale=""showall"" wmode=""opaque"" devicefont=""false"" bgcolor=""#ffffff"" name=""doc_296323{0}_object"" menu=""true"" allowfullscreen=""true"" allowscriptaccess=""always"" salign="""" type=""application/x-shockwave-flash"" align=""middle""  height=""{5}"" width=""{6}""></embed>", document.DocumentId.ToString(), document.AccessKey, startingPage.ToString(), "1", _viewMode, viewHeight, viewWidth);
            _sb.Append(@"</object>");
            _sb.Append(@"<div style=""font-size:10px;text-align:center;width:100%"">");
            _sb.AppendFormat(@"<a href=""http://www.scribd.com/doc/{0}"">{1}</a> - <a href=""http://www.scribd.com/upload"">Upload a Document to Scribd</a></div><div style=""display:none""> Read this document on Scribd: <a href=""http://www.scribd.com/doc/{0}"">{1}</a> </div>", document.DocumentId.ToString(), document.Title);

            return _sb.ToString();
        }

    }
}
